package com.baosight.risk.service.xtri.impl;

import java.util.List;
import java.util.Map;

import com.baosight.risk.api.system.ISysRoleService;
import com.baosight.risk.common.utils.IDGenerator;
import com.baosight.risk.mapper.xtaa.code.XtaaCodeMapper;
import com.baosight.risk.service.xtaa.code.entity.XtaaCode;
import com.baosight.risk.service.xtri.entity.TXtriTempRisk;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baosight.risk.api.xtri.IXtriTempActiveService;
import com.baosight.risk.base.mapper.BaseMapper;
import com.baosight.risk.base.service.BaseServiceImpl;
import com.baosight.risk.common.bean.BeanUtils;
import com.baosight.risk.common.utils.HttpClientUtil;
import com.baosight.risk.mapper.xtri.TXtriTempActiveMapper;
import com.baosight.risk.service.xtri.entity.TXtriTempActive;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.util.ObjectUtils;

@Service("xtriTempActiveServiceImpl")
public class XtriTempActiveServiceImpl extends BaseServiceImpl<TXtriTempActive> implements IXtriTempActiveService {
    @Autowired
    private TXtriTempActiveMapper tempActiveMapper;

	@Autowired
	private XtaaCodeMapper xtaaCodeMapper;
    
    @Override
    public BaseMapper getMapper() {
        return tempActiveMapper;
    }
    
	@Override
	@Transactional
	public void importActive() throws Exception {
		XtaaCode codeParam=new  XtaaCode();
		codeParam.setDdTypeCode("XTRI_TEMP_URL");
		codeParam.setDdCode("QUERY_SJ_2");
		List<XtaaCode> codeList=xtaaCodeMapper.select(codeParam);
		if(codeList.size()>0){
			String url = codeList.get(0).getDdDesc();
			String result = HttpClientUtil.doPostJson(url,"");
			JSONObject js = JSONObject.fromObject(result);

			if ("1".equals(js.getString("status"))) {
				//清空数据
				tempActiveMapper.deleteActiveAll();
				JSONArray jSONArray = js.getJSONArray("result");
				for(int i=0;i<jSONArray.size();i++){
					Map m = jSONArray.getJSONObject(i);
					TXtriTempActive tempActive = BeanUtils.map2Object(m, TXtriTempActive.class);
					this.save(tempActive);
				}
			}else{
				throw new Exception(js.getString("msg"));
			}
		}

	}

	@Override
	public TXtriTempActive save(TXtriTempActive tempActive) {
		if ("继续适用".equals(tempActive.getFileStatus())) {
			tempActive.setFileStatusName(tempActive.getFileStatus());
			tempActive.setFileStatus("10");
		} else if ("修订".equals(tempActive.getFileStatus())) {
			tempActive.setFileStatusName(tempActive.getFileStatus());
			tempActive.setFileStatus("20");
		} else if ("作废".equals(tempActive.getFileStatus())) {
			tempActive.setFileStatusName(tempActive.getFileStatus());
			tempActive.setFileStatus("30");
		} else if ("新增".equals(tempActive.getFileStatus())) {
			tempActive.setFileStatusName(tempActive.getFileStatus());
			tempActive.setFileStatus("40");
		}
		tempActive = (TXtriTempActive) BeanUtils.initBean(tempActive);
		try {
			tempActive.setInternalCode(IDGenerator.getIdStr());
			tempActiveMapper.insertTempActive(tempActive);
			return tempActive;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;

	}

	@Override
	public List<TXtriTempActive> queryTempActive(TXtriTempActive param) {
		servicePage();
		return tempActiveMapper.selectActive(param);
	}

}
